package com.stwitinc.redmine;

import com.alibaba.fastjson.JSONObject;
import com.stwitinc.redmine.dao.DAO;
import com.stwitinc.servlet.InvalidTokenException;

public class TokenValidator implements com.stwitinc.servlet.TokenValidator
{
	@Override
	public JSONObject validate(String tokenId) throws Exception {
		if (tokenId == null) {
			return null;
		}
		if ("f21be82296844ad1721e2606da63c26e83c30231780fb78456a7836dcca0e05f".equals(tokenId)) {
			JSONObject token = new JSONObject();
			token.put("tokenId", tokenId);
			token.put("userId", "Internal Invoker");
			return token;
		}
		JSONObject obj = DAO.newUserDAO().getTokenById(tokenId);
		if (obj == null) {
			throw new InvalidTokenException(tokenId);
		}
		JSONObject token = new JSONObject();
		token.put("tokenId", obj.getString("tokenId"));
		token.put("userId", obj.getString("userId"));
		return token;
	}
}
